﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class CourseManageBySchoolAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "School")
            {
                Response.Redirect("~/Search.aspx");
            }
            if (SQLHelper.getSchoolIdBySchoolAccountId(((Entity.UserInfo)Session["UserInfo"]).UserId) == -1)
            {
                Response.Write("<script>alert('尚未绑定学校，请联系管理员!');location.href='WelcomeSchoolAccount.aspx';</script>");
                return;
            }
            Session.Add("SchoolId", SQLHelper.getSchoolIdBySchoolAccountId(((Entity.UserInfo)Session["UserInfo"]).UserId));
            if (!IsPostBack)
            {
                if (Session["SchoolId"] == null)
                {
                    Response.Redirect("~/Search.aspx");
                }
                int iSchoolId = (int)Session["SchoolId"];
                DataTable dt = new DataTable();
                SQLHelper.getCourseByShoolId(iSchoolId, out dt);
                gdvCourse.DataSource = dt;
                gdvCourse.DataKeyNames = new string[] { "Id" };
                gdvCourse.DataBind();
                Session.Remove("CourseId");
            }
        }
        

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session.Remove("CourseId");
            Response.Redirect("CourseEditBySchoolAccount.aspx");
        }

        protected void gdvCourse_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
        }

        protected void gdvCourse_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = Convert.ToInt32(gdvCourse.DataKeys[e.RowIndex].Value);
            SQLHelper.deleteCourseById(i);
            Response.Redirect("CourseManageBySchoolAccount.aspx");
        }

        protected void gdvCourse_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int i = Convert.ToInt32(gdvCourse.DataKeys[e.NewEditIndex].Value);
            Session.Add("CourseId", i);
            Response.Redirect("CourseEditBySchoolAccount.aspx");
        }
    }
}